<?php


namespace app\services\alipay;
use app\home\model\Config;



class Offiaccount
{

    public function __construct()
    {

        $config = array(
            //支付宝公钥
            'alipay_public_key' => Config::getConfigValue('alipay_public_key'),

            //商户私钥
            'merchant_private_key' => Config::getConfigValue('alipay_merchant_private_key'),
            //编码格式
            'charset' => "UTF-8",

            //支付宝网关
            'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
            'sign_type' => 'RSA2',
            //应用ID
            'app_id' => Config::getConfigValue('alipay_app_id'),
            'MaxQueryRetry' => "5",
            'QueryDuration' => "3",
            'notify_url' => '',
        );
        $this->config = $config;
    }

    public function paymentCode($authCode, $array)
    {
        $data['scene'] = 'bar_code'; //支付场景
        $data['out_trade_no'] = $array['sn_order'];  //订单号
        $data['total_amount'] = $array['reality_amount']; //支付金额
        $data['auth_code'] = $authCode;   //支付授权码
        $data['subject'] = '消费扣款'; //订单标题
        $data['body'] = '订单说明'; //订单说明
        $data = json_encode($data);

        $params['app_id'] = $this->config['app_id'];
        $params['method'] = 'alipay.trade.pay';
        $params['charset'] = 'utf-8';
        $params['sign_type'] = 'RSA2';
        $params['timestamp'] = date("Y-m-d H:i:s");
        $params['version'] = '1.0';
        $params['notify_url'] = "";
        $params['biz_content'] = $data;
        ksort($params); //对将要签名的数组排序
        $string = $this->toUrlParam($params); //将数组转换成字符串
        $params['sign'] = $this->alipay_sign($string); //将字符串签名
        $url = 'https://openapi.alipay.com/gateway.do?' . http_build_query($params);
        $return = file_get_contents($url); //打开支付请求连接 获取二维码地址

        $return = json_decode($return, true); //将返回的字符串转换为数组
        if($return['alipay_trade_pay_response']['code'] == '10000'){
            return ['code' => 1,'msg'=> '支付拉起成功'];
        }else{
            return ['code' => 0,'msg'=>'支付拉起失败'];
        }
        return $return;

    }


    public function pay($array,$subject,$notify_url)
    {
        include_once APP_PATH.'/../extend/alipay/aop/AopClient.php';
        $aop = new \AopClient();
        $aop->gatewayUrl  = "https://openapi.alipay.com/gateway.do";
        $aop->appId =  $this->config['app_id'];
        $aop->postCharset  = 'utf-8';
        $aop->signType = 'RSA2';
        $aop->apiVersion = '1.0';
        $aop->alipayrsaPublicKey = $this->config['alipay_public_key'];
        $aop->rsaPrivateKey       = $this->config['merchant_private_key'];
        $data['out_trade_no'] = $array['sn_order'];  //订单号
        $data['total_amount'] = $array['reality_amount']; //支付金额
        if ($subject){
            $data['subject'] = $subject; //订单标题
        }else{
            $data['subject'] = '消费扣款';
        }
        $data['subject'] = '消费扣款';
        $data['body'] = ''; //订单说明
        $json = json_encode($data);

        include_once APP_PATH.'/../extend/alipay/aop/request/AlipayTradeWapPayRequest.php';

        $pay = new \AlipayTradeWapPayRequest();
        $pay->setNotifyUrl(getProtocol() . $_SERVER["HTTP_HOST"] . '/home.php/' . $notify_url);
        $pay->setBizContent($json);
        $pageRedirectionData = $aop->pageExecute ($pay,"POST");
    // 如果需要返回GET请求，请使用
    // $pageRedirectionData = $aop->pageExecute ($request,"GET");
        return $pageRedirectionData;
        /*
         *
        $data['out_trade_no'] = $array['sn_order'];  //订单号
        $data['total_amount'] = $array['reality_amount']; //支付金额
        if ($subject){
            $data['subject'] = $subject; //订单标题
        }else{
            $data['subject'] = '消费扣款';
        }
        $data['subject'] = '消费扣款';
        $data['body'] = ''; //订单说明
        $data = json_encode($data);
        $params['app_id'] = $this->config['app_id'];
        $params['method'] = 'alipay.trade.wap.pay';
        $params['charset'] = 'utf-8';
        $params['sign_type'] = 'RSA2';
        $params['notify_url'] = getProtocol() . $_SERVER["HTTP_HOST"] . '/home.php/' . $notify_url;;
        $params['timestamp'] = date("Y-m-d H:i:s");
        $params['version'] = '1.0';
        $params['notify_url'] = "";
        $params['biz_content'] = $data;
        ksort($params); //对将要签名的数组排序
        $string = $this->toUrlParam($params); //将数组转换成字符串
        $params['sign'] = $this->alipay_sign($string); //将字符串签名
        $url = 'https://openapi.alipay.com/gateway.do?' . http_build_query($params);
        $return = file_get_contents($url); //打开支付请求连接 获取二维码地址
        $return = json_decode($return, true); //将返回的字符串转换为数组
        return $return;*/
    }

    public function orderquery($array){

        $data['out_trade_no'] = $array['sn_order'];  //订单号
        $data = json_encode($data);
        $params['app_id'] = $this->config['app_id'];
        $params['method'] = 'alipay.trade.query';
        $params['charset'] = 'utf-8';
        $params['sign_type'] = 'RSA2';
        $params['timestamp'] = date("Y-m-d H:i:s");
        $params['version'] = '1.0';
        $params['notify_url'] = "";
        $params['biz_content'] = $data;
        ksort($params); //对将要签名的数组排序
        $string = $this->toUrlParam($params); //将数组转换成字符串
        $params['sign'] = $this->alipay_sign($string); //将字符串签名
        $url = 'https://openapi.alipay.com/gateway.do?' . http_build_query($params);
        $return = file_get_contents($url); //打开支付请求连接 获取二维码地址
        $return = json_decode($return, true); //将返回的字符串转换为数组
        return  $return;

    }

//app支付宝签名
    public function alipay_sign($data, $signType = "RSA2"){
            $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
                wordwrap($this->config['merchant_private_key'], 64, "\n", true) .
                "\n-----END RSA PRIVATE KEY-----";
            ($res) or die('您使用的私钥格式错误，请检查RSA私钥配置');
            if ("RSA2" == $signType) {
                openssl_sign($data, $sign, $res, "SHA256");
            } else {
            openssl_sign($data, $sign, $res);
        }
        $sign = base64_encode($sign);
        return $sign;

    }
    public  function toUrlParam($param){
        $buff = "";
        foreach($param as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k."=".$v."&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }
    public  function refund($array){
        $data['out_trade_no'] = $array['sn_order'];  //订单号
        $data['refund_amount'] = $array['reality_amount'];  //订单号
        $data = json_encode($data);
        $params['app_id'] = $this->config['app_id'];
        $params['method'] = 'alipay.trade.refund';
        $params['charset'] = 'utf-8';
        $params['sign_type'] = 'RSA2';
        $params['timestamp'] = date("Y-m-d H:i:s");
        $params['version'] = '1.0';
        $params['notify_url'] = "";
        $params['biz_content'] = $data;
        ksort($params); //对将要签名的数组排序
        $string = $this->toUrlParam($params); //将数组转换成字符串
        $params['sign'] = $this->alipay_sign($string); //将字符串签名
        $url = 'https://openapi.alipay.com/gateway.do?' . http_build_query($params);
        $return = file_get_contents($url); //打开支付请求连接 获取二维码地址
        $return = json_decode($return, true); //将返回的字符串转换为数组
        return  $return;
    }
    public  function refundQuery($array){
        $data['out_request_no'] = $array['sn_order'];  //订单号
        $data['out_trade_no'] = $array['sn_order'];  //订单号
        $data = json_encode($data);
        $params['app_id'] = $this->config['app_id'];
        $params['method'] = 'alipay.trade.fastpay.refund.query';
        $params['charset'] = 'utf-8';
        $params['sign_type'] = 'RSA2';
        $params['timestamp'] = date("Y-m-d H:i:s");
        $params['version'] = '1.0';
        $params['notify_url'] = "";
        $params['biz_content'] = $data;
        ksort($params); //对将要签名的数组排序
        $string = $this->toUrlParam($params); //将数组转换成字符串
        $params['sign'] = $this->alipay_sign($string); //将字符串签名
        $url = 'https://openapi.alipay.com/gateway.do?' . http_build_query($params);
        $return = file_get_contents($url); //打开支付请求连接 获取二维码地址

        $return = json_decode($return, true); //将返回的字符串转换为数组
        return  $return;
    }


}